package cn.tradewin.advisor.commons.selectmodel;

import java.util.List;

import org.apache.tapestry5.ValueEncoder;

import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.services.CodetableService;

public class CodeLabelEncoder implements ValueEncoder<CodeLabel> {

	private CodetableService service;
	private String category;
	
	public CodeLabelEncoder(CodetableService service, String category) {
		this.service = service;
		this.category = category;
	}
	
	@Override
	public String toClient(CodeLabel value) {
		return value.getCode();
	}

	@Override
	public CodeLabel toValue(String clientValue) {
		List<CodeLabel> codeLabels = service.listCategory(category, "zh");
		for (CodeLabel cl : codeLabels) {
			if (cl.getCode().equals(clientValue)) {
				System.out.println("find the codelabel************************************************************************");
				return cl;
			}
		}
		return null;
	}

}
